package org.stvd.controller.common;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.stvd.common.Consts;
import org.stvd.common.aspectj.lang.annotation.Log;
import org.stvd.common.aspectj.lang.enums.BusinessType;
import org.stvd.common.utils.InterfaceResult;
import org.stvd.service.common.MailSendService;
import org.stvd.service.common.MsgSendHisService;

/**
 * 验证码发送控制类
 * @author houzx
 */
@Controller
@RequestMapping("validatecode")
public class VerifyCodeController{

    @Autowired
    private MsgSendHisService msgSendHisService; //短信发送Service
    @Autowired
    private MailSendService mailSendService; //邮件Service

    /**
     * 注册时获取验证码
     */
    @Log(title = "发送手机注册验证码", businessType = BusinessType.OTHER)
    @ResponseBody
    @RequestMapping(value="registermobilecode", method=RequestMethod.GET)
    public InterfaceResult<Map<String, Object>> sendMobileVcodeForRegister(HttpServletRequest request) {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<>();
        String moblieNumStr = request.getParameter("mobile_num");
        result.transferResult(msgSendHisService.sendAndRegisterMobileCode(moblieNumStr, Consts.GUID));
        return result;
    }
    
    /**
     * 绑定手机时获取验证码
     */
    @Log(title = "发送手机绑定验证码", businessType = BusinessType.OTHER)
    @ResponseBody
    @RequestMapping(value="bindingmobilecode", method=RequestMethod.GET)
    public InterfaceResult<Map<String, Object>> sendMobileVcodeForBinding(HttpServletRequest request) {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<>();
        String moblieNumStr = request.getParameter("mobile_num");
        result.transferResult(msgSendHisService.sendAndRegisterMobileCode(moblieNumStr, Consts.GUID));
        return result;
    }
    
    /**
     * 重置密码时获取验证码
     */
    @Log(title = "发送重置密码手机验证码", businessType = BusinessType.OTHER)
    @ResponseBody
    @RequestMapping(value="resetpwdbymobilecode", method=RequestMethod.GET)
    public InterfaceResult<Map<String, Object>> sendMobileVcodeForResetPassword(HttpServletRequest request) {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<>();
        String moblieNumStr = request.getParameter("mobile_num");
        result.transferResult(msgSendHisService.sendAndResetPasswordMobileCode(moblieNumStr, Consts.GUID));
        return result;
    }
    
    /**
     * 绑定邮箱时发送验证码邮件
     */
    @Log(title = "发送邮箱绑定验证码", businessType = BusinessType.OTHER)
    @ResponseBody
    @RequestMapping(value="bindingemailcode", method=RequestMethod.GET)
    public InterfaceResult<Map<String, Object>> sendEmailVcodeForBinding(HttpServletRequest request) {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<>();
        String emailNumStr = request.getParameter("email_num");
        result.transferResult(mailSendService.sendAndBindingEmailCode(Consts.SYSTEM_NAME, emailNumStr));
        return result;
    }
    
    /**
     * 重置密码时发送验证码邮件
     */
    @Log(title = "发送重置密码邮箱验证码", businessType = BusinessType.OTHER)
    @ResponseBody
    @RequestMapping(value="resetpwdbyemailcode", method=RequestMethod.GET)
    public InterfaceResult<Map<String, Object>> sendEmailVcodeForResetPassword(HttpServletRequest request) {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<>();
        String emailNumStr = request.getParameter("email_num");
        result.transferResult(mailSendService.sendAndResetPasswordEmailCode(Consts.SYSTEM_NAME, emailNumStr));
        return result;
    }

}
